<?php
if ( ! defined('ROOT')) exit('No direct script access allowed');

/**
 * 后台模型类
 * @author gzute
 * @link http://www.gzute.com
 */

class Admin extends InterfaceApp
{
	public static $router;
	public static $var = array();
	
	public function __construct()
	{
		parent::__construct();
		self::init_start();
		self::$router = Base::load_sys_class('router');
		self::check_admin_login();
		Base::load_app_func('menus');
	}
	
	public static function init_start()
	{
		header("Pragma: no-cache");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		defined('JS_PATH') or define('JS_PATH', base_url().'libs/modules/admin/templates/js/');
		defined('CSS_PATH') or define('CSS_PATH', base_url().'libs/modules/admin/templates/style/');
		defined('IMG_PATH') or define('IMG_PATH', base_url().'libs/modules/admin/templates/style/images/');
	}
	
	/**
	 * 检查是否已登陆后台
	 * Enter description here ...
	 */
	public static function check_admin_login()
	{
		$admin_id = self::get_session('admin_id');
		$admin_name = self::get_session('admin_name');
		$check_method = array('login','captcha');
		$method = self::$router->get_action(); //获取方法
		if(!$admin_id && !$admin_name && !in_array($method, $check_method))
		{
			$url = array(
				'm' => 'admin',
				'c' => 'index',
				'a' => 'login'
			);
			redirect($url, 'refresh');
		}
	}
	
	public static function tpl_assign($item, $value = '')
	{
		if(is_array($item))
		{
			self::$var = $item;
		}else {
			self::$var[$item] = $value; 
		}
		return true;
	}
	
	/**
	 * 读取后台模板
	 * @param $tpl_name 模板名称
	 * @param $m 模型目录
	 */
	public static function tpl_display($tpl_name,$m = '')
	{
		$config = Base::load_config('config','','',true);
		extract($config);
		if (count(self::$var) > 0)
		{
			extract(self::$var);
		}
		if(empty($m) || $m == '')
		{
			$m = self::$router->get_module();
		}
		include LIB_PATH.'modules/'.$m.'/templates/'.$tpl_name.'.tpl.php';
	}
}

//end of this file